﻿using CPPEI.Coolzon.Common;
using CPPEI.Coolzon.Service;
using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.Text;
using System.Threading.Tasks;

namespace CPPEI.Coolzon.ServiceImp
{
    [ServiceBehavior(InstanceContextMode = InstanceContextMode.PerCall)]
    public class PublishService : IPublishService
    {
        public PreparePublishSolutionResponse PreparePublishSolution(PreparePublishSolutionRequest request)
        {
            Logger.DebugToTag(Library.LogPublishService, string.Format("准备发布方案:{0}", request.Solution));
            PreparePublishSolutionResponse response = new PreparePublishSolutionResponse();

            // 创建方案文件存储的临时目录
            Guid templateId = Guid.NewGuid();
            string trancactionId = string.Format("_{0}", templateId.ToString("N"));

            if(Runtime.Metadata.Storage.CreateSolutionDirectory(trancactionId))
            {
                Logger.DebugToTag(Library.LogPublishService, string.Format("准备发布方案:{0}成功", request.Solution));
                response.IsSuccess = true;
                response.ErrorCode = 0;
                response.Message = string.Empty;
                response.TrancactionId = trancactionId;

                PublishEventBus.Publish(new BeginPublishSolution() { Name = request.Solution });
            }
            else
            {
                Logger.DebugToTag(Library.LogPublishService, string.Format("准备发布方案:{0}失败", request.Solution));
                response.IsSuccess = false;
                response.ErrorCode = 1;
                response.Message = "create template solution directory failure.";
                response.TrancactionId = string.Empty;
            }
            return response;
        }

        public CommitPublishSolutionResponse CommitPublishSolution(CommitPublishSolutionRequest request)
        {
            Logger.DebugToTag(Library.LogPublishService, string.Format("提交发布方案:{0}", request.Solution));
            // 将方案文件从临时目录拷贝到实际的目录
            CommitPublishSolutionResponse response = new CommitPublishSolutionResponse();
            // 将文件夹移动到实际目录
            if (Runtime.Metadata.Storage.RenameSolutionDirectory(request.TrancactionId, request.Solution))
            {
                if(Runtime.Metadata.Storage.UpdateSolutionSummary(request.Solution, request.Caption, request.Version, request.RTVersion, request.Type))
                {
                    Logger.DebugToTag(Library.LogPublishService, string.Format("提交发布方案:{0}成功", request.Solution));


                    response.IsSuccess = true;
                    response.ErrorCode = 0;
                    response.Message = string.Empty;
                }
                else
                {
                    Logger.DebugToTag(Library.LogPublishService, string.Format("提交发布方案:{0}失败", request.Solution));

                    response.IsSuccess = false;
                    response.ErrorCode = 1;
                    response.Message = "commit publish solution failure.";
                }
            }
            else
            {
                Logger.DebugToTag(Library.LogPublishService, string.Format("提交发布方案:{0}失败", request.Solution));

                response.IsSuccess = false;
                response.ErrorCode = 1;
                response.Message = "commit publish solution failure.";
            }

            if(response.IsSuccess)
            {
                PublishEventBus.Publish(new EndPublishSolution() { IsSuccess = true, Name = request.Solution});
            }
            else
            {
                PublishEventBus.Publish(new EndPublishSolution() { IsSuccess = false, Name = request.Solution });
            }
            return response;
        }

        public PublishSolutionFileResponse PublishSolutionFile(PublishSolutionFileRequest request)
        {
            Logger.DebugToTag(Library.LogPublishService, string.Format("发布文件:{0},方案:{1}", request.FileName, request.Solution));
            PublishSolutionFileResponse response = new PublishSolutionFileResponse();
            // 向临时目录里生成文件

            if (Runtime.Metadata.Storage.SaveSolutionFile(request.TrancactionId, request.FileName, request.Content))
            {
                Logger.DebugToTag(Library.LogPublishService, string.Format("发布文件:{0},方案:{1}成功", request.FileName, request.Solution));
                response.IsSuccess = true;
                response.ErrorCode = 0;
                response.Message = string.Empty;
                response.FileName = request.FileName;
            }
            else
            {
                Logger.DebugToTag(Library.LogPublishService, string.Format("发布文件:{0},方案:{1}失败", request.FileName, request.Solution));
                response.IsSuccess = false;
                response.ErrorCode = 1;
                response.Message = "save file failure.";
                response.FileName = request.FileName;
            }
            return response;
        }

        public CancelPublishSolutionResponse CancelPublishSolution(CancelPublishSolutionRequest request)
        {
            // 将方案文件从临时目录拷贝到实际的目录
            CancelPublishSolutionResponse response = new CancelPublishSolutionResponse();

            // 将文件夹移动到实际目录
            if (Runtime.Metadata.Storage.DeleteSolutionDirectory(request.TrancactionId))
            {
                response.IsSuccess = true;
                response.ErrorCode = 0;
                response.Message = string.Empty;
            }
            else
            {
                response.IsSuccess = false;
                response.ErrorCode = 1;
                response.Message = "cancel publish solution failure.";
            }
            PublishEventBus.Publish(new EndPublishSolution() { IsSuccess = false, Name = request.Solution});
            return response;
        }

        #region data source

        public PreparePublishDataSourceResponse PreparePublishDataSource(PreparePublishDataSourceRequest request)
        {
            PreparePublishDataSourceResponse response = new PreparePublishDataSourceResponse();

            // 创建方案文件存储的临时目录
            Guid templateId = Guid.NewGuid();
            string trancactionId = string.Format("_{0}", templateId.ToString("N"));

            response.IsSuccess = true;
            response.ErrorCode = 0;
            response.Message = string.Empty;
            response.TrancactionId = trancactionId;

            return response;
        }

        public CommitPublishDataSourceResponse CommitPublishDataSource(CommitPublishDataSourceRequest request)
        {
            // 将方案文件从临时目录拷贝到实际的目录
            CommitPublishDataSourceResponse response = new CommitPublishDataSourceResponse();
            if (string.IsNullOrEmpty(request.Solution))
            {
                // 将文件夹移动到实际目录
                if (Runtime.Metadata.Storage.RenameDataSourceFile(request.TrancactionId, request.DataSource))
                {
                    response.IsSuccess = true;
                    response.ErrorCode = 0;
                    response.Message = string.Empty;
                }
                else
                {
                    response.IsSuccess = false;
                    response.ErrorCode = 1;
                    response.Message = "commit publish datasource failure.";
                }
            }
            else
            {
                if (Runtime.Metadata.Storage.RenameDataSourceFile(request.Solution, request.TrancactionId, request.DataSource))
                {
                    response.IsSuccess = true;
                    response.ErrorCode = 0;
                    response.Message = string.Empty;
                }
                else
                {
                    response.IsSuccess = false;
                    response.ErrorCode = 1;
                    response.Message = "commit publish datasource failure.";
                }
            }
            return response;
        }

        public PublishDataSourceFileResponse PublishDataSourceFile(PublishDataSourceFileRequest request)
        {
            PublishDataSourceFileResponse response = new PublishDataSourceFileResponse();
            // 向临时目录里生成文件
            if(string.IsNullOrEmpty(request.Solution))
            {
                // save to shared
                if (Runtime.Metadata.Storage.SaveDataSourceFile(request.TrancactionId, request.Content))
                {
                    response.IsSuccess = true;
                    response.ErrorCode = 0;
                    response.Message = string.Empty;
                    response.FileName = request.DataSource;
                }
                else
                {
                    response.IsSuccess = false;
                    response.ErrorCode = 1;
                    response.Message = "save file failure.";
                    response.FileName = request.DataSource;
                }
            }
            else
            {
                if (Runtime.Metadata.Storage.SaveDataSourceFile(request.Solution, request.TrancactionId, request.Content))
                {
                    response.IsSuccess = true;
                    response.ErrorCode = 0;
                    response.Message = string.Empty;
                    response.FileName = request.DataSource;
                }
                else
                {
                    response.IsSuccess = false;
                    response.ErrorCode = 1;
                    response.Message = "save file failure.";
                    response.FileName = request.DataSource;
                }
            }
            return response;
        }

        #endregion


        public CancelPublishDataSourceResponse CancelPublishDataSource(CancelPublishDataSourceRequest request)
        {
            CancelPublishDataSourceResponse response = new CancelPublishDataSourceResponse();
            if (string.IsNullOrEmpty(request.Solution))
            {
                // 将文件夹移动到实际目录
                if (Runtime.Metadata.Storage.DeleteDataSourceFile(request.TrancactionId))
                {
                    response.IsSuccess = true;
                    response.ErrorCode = 0;
                    response.Message = string.Empty;
                }
                else
                {
                    response.IsSuccess = false;
                    response.ErrorCode = 1;
                    response.Message = "cancel publish datasource failure.";
                }
            }
            else
            {
                if (Runtime.Metadata.Storage.DeleteDataSourceFile(request.Solution, request.TrancactionId))
                {
                    response.IsSuccess = true;
                    response.ErrorCode = 0;
                    response.Message = string.Empty;
                }
                else
                {
                    response.IsSuccess = false;
                    response.ErrorCode = 1;
                    response.Message = "cancel publish datasource failure.";
                }
            }
            return response;
        }
    }
}
